/*
 * ReferenceTable.h
 *
 *  Created on: 2011-7-7
 *      Author: gexiao
 */

#ifndef REFERENCETABLE_H_
#define REFERENCETABLE_H_
#include "oo/Object.h"
#include <stdbool.h>
/*
 * Table definition.
 *
 * The expected common operations are adding a new entry and removing a
 * recently-added entry (usually the most-recently-added entry).
 *
 * If "allocEntries" is not equal to "maxEntries", the table may expand when
 * entries are added, which means the memory may move. If you want to keep
 * pointers into "table" rather than offsets, use a fixed-size table.
 *
 * (This structure is still somewhat transparent; direct access to
 * table/nextEntry is allowed.)
 */
typedef struct ReferenceTable {
	Object** table; //top of the list
	Object** nextEntry; //bottom of the list

	int allocEntries; //#of entries we have space for
	int maxEntries; //max #of entries allowed
} ReferenceTable;

/*
 * Initialize a ReferenceTable.
 *
 * If "initialCount"!="maxCount",the table will expand as required.
 *
 * Returns "false" if table allocation fails.
 */bool dvmInitReferenceTable(ReferenceTable* pRef, int initialCount,
		int maxCount);

/*
 *
 */
void dvmClearReferenceTable(ReferenceTable* pRef);

/*
 * Return the #of entries currently stored in the ReferenceTable.
 */
INLINE size_t dvmReferenceTableEntries(const ReferenceTable* pRef) {
	return pRef->nextEntry - pRef->table;
}

/*
 * Add a new entry.  "obj" must be a valid non-NULL object reference
 * (though it's okay if it's not fully-formed, e.g. the result from
 * dvmMalloc doesn't have obj->clazz set).
 *
 * Returns "false" if the table is full.
 */
bool dvmAddToReferenceTable(ReferenceTable* pRef, Object* obj);
#endif /* REFERENCETABLE_H_ */
